package org.smart4j.chapter2.service;

import com.sun.org.apache.bcel.internal.generic.FADD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.chapter2.helper.DatabaseHelper;
import org.smart4j.chapter2.model.Customer;
import org.smart4j.chapter2.util.PropsUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by huangxiaofeng on 2015/11/13.
 */
public class CustomerService {
    private static Logger logger = LoggerFactory.getLogger(CustomerService.class);

    //    private static final String DRIVER;
//    private static final String URL;
//    private static final String USERNAME;
//    private static final String PAASSWORD;
//    static {
//        Properties conf= PropsUtil.loadProps("config.properties");
//        DRIVER=conf.getProperty("jdbc.driver");
//        URL=conf.getProperty("jdbc.url");
//        USERNAME=conf.getProperty("jdbc.username");
//        PAASSWORD=conf.getProperty("jdbc.password");
//        try{
//            Class.forName(DRIVER);
//        }catch (ClassNotFoundException e){
//            logger.error("can not load jdbc driver",e);
//        }
//    }
    public List<Customer> getCustomerList() {
//       Connection conn=null;
//        try{
//          List<Customer> customerList=new ArrayList<Customer>();
        String sql = "select * from customer";
//            conn= DriverManager.getConnection(URL,USERNAME,PAASSWORD);
//            conn= DatabaseHelper.getConnection();
        return DatabaseHelper.queryEntityList(Customer.class, sql);
//            PreparedStatement stmt=conn.prepareStatement(sql);
//            ResultSet rs=stmt.executeQuery();
//            while(rs.next()){
//                Customer customer=new Customer();
//                customer.setId(rs.getLong("id"));
//                customer.setName(rs.getString("name"));
//                customer.setContact(rs.getString("contact"));
//                customer.setTelephone(rs.getString("telephone"));
//                customer.setEmail(rs.getString("email"));
//                customer.setRemark(rs.getString("remark"));
//                customerList.add(customer);
//            }
//            return  customerList;
//        }
//        catch(SQLException e){
//            logger.error("execute sql failure",e);
//        }
//        finally {
////            if(conn!=null){
////                try{
////                    conn.close();
////                }catch (SQLException e){
////                    logger.error("close connection failur",e);
////                }
////            }
//            DatabaseHelper.closeConnection(conn);
//    }
//        return null;
    }

    public Customer getCustomer(long id) {
        //TODO
        return null;
    }

    public boolean createCustomer(Map<String, Object> fieldMap) {
        return DatabaseHelper.insertEntity(Customer.class,fieldMap);
    }

    public boolean updateCustomer(long id, Map<String, Object> fieldMap) {
        return DatabaseHelper.updateEntity(Customer.class,id,fieldMap);
    }

    public boolean deleteCustomer(long id) {
        //TODO
        return DatabaseHelper.deleteEntity(Customer.class,id);
    }
}
